package org.jeecg.modules.demo.analysis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.modules.demo.analysis.entity.ZnzfCountTurnNumberParam;
import org.jeecg.modules.demo.analysis.entity.ZnzfTurn;
import org.jeecg.modules.demo.analysis.mapper.ZnzfTurnMapper;
import org.jeecg.modules.demo.analysis.service.IZnzfTurnService;
import org.jeecg.modules.demo.projectroom.entity.ZnzfProject;
import org.jeecg.modules.demo.projectroom.mapper.ZnzfProjectMapper;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 轮候表，供数据分析使用
 * @Author: jeecg-boot
 * @Date:   2022-04-10
 * @Version: V1.0
 */
@Service
public class ZnzfTurnServiceImpl extends ServiceImpl<ZnzfTurnMapper, ZnzfTurn> implements IZnzfTurnService {
    @Resource
    private ZnzfTurnMapper znzfTurnMapper;

    @Resource
    private ZnzfProjectMapper znzfProjectMapper;

    @Override
    public List<ZnzfCountTurnNumberParam> countTurnNumber(String station) {
        if (station != null) {
            QueryWrapper<ZnzfProject> queryWrapper = new QueryWrapper<>();
            queryWrapper.select("id")
                    .eq("station", station);
            List<ZnzfProject> znzfProjectList = znzfProjectMapper.selectList(queryWrapper);
            //判断是否存在当前输入的站段号
            if (znzfProjectList.size() == 0)
                return null;

            List<ZnzfCountTurnNumberParam> znzfCountTurnNumberParamList = new ArrayList<>();
            ZnzfCountTurnNumberParam znzfCountTurnNumberParam = new ZnzfCountTurnNumberParam();
            znzfCountTurnNumberParam.setStation(station);
            Long familyTurnNumber = countTurnNumberByConditions(station, 2, 0);
            Long MenTurnNumber = countTurnNumberByConditions(station, 0, 1);
            Long WomenTurnNumber = countTurnNumberByConditions(station, 0, 2);
            znzfCountTurnNumberParam.setFamilyTurnNumber(familyTurnNumber);
            znzfCountTurnNumberParam.setMenTurnNumber(MenTurnNumber);
            znzfCountTurnNumberParam.setWomenTurnNumber(WomenTurnNumber);

            znzfCountTurnNumberParamList.add(znzfCountTurnNumberParam);

            return znzfCountTurnNumberParamList;
        } else {
            QueryWrapper<ZnzfProject> queryWrapper = new QueryWrapper<>();
            queryWrapper
                    .select("DISTINCT station");
            //获取所有不重复的站段号
            List<ZnzfProject> znzfProjectList = znzfProjectMapper.selectList(queryWrapper);
            //判断是否存在站段号
            if (znzfProjectList == null)
                return null;

            List<ZnzfCountTurnNumberParam> znzfCountTurnNumberParamList = new ArrayList<>();
            for (int i = 0; i < znzfProjectList.size(); i ++) {
                //获取每个不重复的站段号
                String station_temp = znzfProjectList.get(i).getStation();

                ZnzfCountTurnNumberParam znzfCountTurnNumberParam = new ZnzfCountTurnNumberParam();
                znzfCountTurnNumberParam.setStation(station_temp);
                Long familyTurnNumber = countTurnNumberByConditions(station_temp, 2, 0);
                Long MenTurnNumber = countTurnNumberByConditions(station_temp, 0, 1);
                Long WomenTurnNumber = countTurnNumberByConditions(station_temp, 0, 2);
                znzfCountTurnNumberParam.setFamilyTurnNumber(familyTurnNumber);
                znzfCountTurnNumberParam.setMenTurnNumber(MenTurnNumber);
                znzfCountTurnNumberParam.setWomenTurnNumber(WomenTurnNumber);

                znzfCountTurnNumberParamList.add(znzfCountTurnNumberParam);
            }
            return znzfCountTurnNumberParamList;
        }
    }

    @Override
    public ZnzfTurn searchByEmployeeId(String eid) {
        LambdaQueryWrapper<ZnzfTurn> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnzfTurn::getEid, eid);
        return znzfTurnMapper.selectOne(queryWrapper);
    }

    private Long countTurnNumberByConditions(String station, int roomType, int sex) {
        if (station != null) {
            QueryWrapper<ZnzfProject> queryWrapper = new QueryWrapper<>();
            queryWrapper.select("id")
                    .eq("station", station);
            List<ZnzfProject> znzfProjectList = znzfProjectMapper.selectList(queryWrapper);
            List<String> projectIdList = new ArrayList<>();
            for (int i = 0; i < znzfProjectList.size(); i++) {
                String projectId = znzfProjectList.get(i).getId();
                projectIdList.add(projectId);
            }

            Long num = 0L;
            for (String projectId : projectIdList) {
                LambdaQueryWrapper<ZnzfTurn> queryWrapper1 = new LambdaQueryWrapper<>();
                queryWrapper1
                        .eq(ZnzfTurn::getProjectid, projectId);
                if (roomType == 2) {
                    queryWrapper1
                            .eq(ZnzfTurn::getType, roomType);
                } else if (roomType == 0) {
                    queryWrapper1
                            .eq(ZnzfTurn::getType, roomType)
                            .eq(ZnzfTurn::getSex, sex);
                }
                num += znzfTurnMapper.selectCount(queryWrapper1);
            }
            return num;
        } else {
            System.out.println("station为空！");
            return 0L;
        }
    }
}
